//
//  GuideMiddle.m
//  BeautifulShoppings
//
//  Created by GivenBmli on 16/9/13.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "GuideMiddle.h"
#import "GuideMiddleCollectionViewCell.h"
#import "NetworkHandler.h"
#import "GuideMiddleModel.h"
#import "GuideMiddleViewController.h"
#import "TAMacro.h"
@implementation GuideMiddle
- (NSMutableArray *)array {
    if (!_array) {
        self.array = [NSMutableArray array];
    }
    return _array;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self  = [super initWithFrame:frame];
    if (self) {
        [self creatSubview:frame];
        [self analysis];
    }
    return self;
}
- (void)creatSubview:(CGRect)frame {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH / 4.5, SCREEN_WIDTH / 4.5);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10); // 内边距
    
    self.collectionViewMiddle = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 3 -  SCREEN_HEIGHT / 5 ) collectionViewLayout:flowLayout
                                 ];
    [self.collectionViewMiddle jxl_setDayMode:^(UIView *view) {
        self.collectionViewMiddle.backgroundColor = [UIColor whiteColor];
    } nightMode:^(UIView *view) {
        self.collectionViewMiddle.backgroundColor = [UIColor grayColor];
    }];
    self.collectionViewMiddle.delegate = self;
    self.collectionViewMiddle.dataSource = self;
    self.collectionViewMiddle.pagingEnabled = YES;
    [self addSubview:_collectionViewMiddle];
    [self.collectionViewMiddle registerClass:[GuideMiddleCollectionViewCell class] forCellWithReuseIdentifier:@"GuideMiddleCell"];
}
- (void)analysis {
    [NetworkHandler getDataByURLString:@"http://api.liwushuo.com/v2/secondary_banners/@/?gender=1&generation=0" BodyString:nil WithDataBlock:^(id result) {
        NSDictionary *dic = [result objectForKey:@"data"];
        NSMutableArray *array = [dic objectForKey:@"secondary_banners"];
        for (NSDictionary *dic in array) {
            GuideMiddleModel *model = [[GuideMiddleModel alloc]init];
            model.image_url = [dic objectForKey:@"image_url"];
            model.target_id = [dic objectForKey:@"target_id"];
            [self.array addObject:model];
        }
        [self.collectionViewMiddle reloadData];
    }];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.array.count;
}
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GuideMiddleCollectionViewCell *GuideMiddleCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GuideMiddleCell" forIndexPath:indexPath];
    GuideMiddleModel *model = self.array[indexPath.item];
    GuideMiddleCell.guideMiddleModel = model;
    
    return GuideMiddleCell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.item ==0) {
        self.strr = @"243";
    } else if (indexPath.item == 2) {
        self.strr = @"246";
    }else if (indexPath.item== 6) {
        self.strr = @"174";
    } else if (indexPath.item == 7) {
        self.strr =@"22";
    } else if (indexPath.item == 8) {
        self.strr = @"1";
    } else if (indexPath.item == 9 ) {
        self.strr = @"245";
    } else if (indexPath.item == 10) {
        self.strr = @"147";
    } else if (indexPath.item == 5) {
        self.strr = @"245";
    } else if (indexPath.item == 3) {
        self.strr = @"247";
    } else if (indexPath.item == 4) {
        self.strr = @"244";
    } else if (indexPath.item == 1) {
        self.strr = @"3";
    } else if (indexPath.item == 11) {
        self.strr = @"22";
    } else if (indexPath.item == 12) {
        self.strr = @"1";
    }
    // 302
    [self.delegate didClickSelectItemAtIndexMiddle:indexPath str:self.strr];
}
@end
